import sceneCouponDialog from './components/scene-coupon-dialog.vue'
import sceneSeckillDialog from './components/scene-seckill-dialog.vue'
import Vue from 'vue'
import store from '@/store'
const notLoginPreviewDialogConstructor = Vue.extend({ ...sceneCouponDialog })
const sceneSeckillDialogConstructor = Vue.extend({ ...sceneSeckillDialog })
const dialogMap = {}
const diyDialog = function (type, params) {
  return new Promise((resolve, reject) => {
    const _constructor = ({
      'sceneCouponDialog': notLoginPreviewDialogConstructor,
      'sceneSeckillDialog': sceneSeckillDialogConstructor
    })[type]
    if (dialogMap[type]) {
      dialogMap[type].$destroy()
      document.body.removeChild(dialogMap[type].$el) // 移除dom
      delete dialogMap[type]
    }
    const dialogDom = new _constructor({
      store,
      el: document.createElement('div')
    })
    dialogMap[type] = dialogDom
    document.body.appendChild(dialogDom.$el) // new一个对象，然后插入body里面
    for (const key in params) {
      dialogDom[key] = params[key]
    }
    dialogDom.exit = function () {
      dialogDom.dialogVisible = false
      document.body.removeChild(dialogDom.$el) // 移除dom
      delete dialogMap[type]
    }
    dialogDom.cancel = function (value) {
      dialogDom.exit()
      reject(value)
    }
    dialogDom.confirm = function (value) {
      dialogDom.exit()
      resolve(value)
    }
  })
}
export default diyDialog

export const hasDialog = (name) => {
  if (dialogMap[name]) {
    return true
  }
}
